package com.byzk.p2p.admin.base.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

public class RequestUtil {
	
	public static String getRemortIP(HttpServletRequest request) { 
		String client_ip = request.getHeader("x-forwarded-for"); //用户IP地址
        if(client_ip == null || client_ip.length() == 0 || "unknown".equalsIgnoreCase(client_ip)) { 
    	   client_ip = request.getHeader("Proxy-Client-IP"); 
        } 
        if(client_ip == null || client_ip.length() == 0 || "unknown".equalsIgnoreCase(client_ip)) { 
    	   client_ip = request.getHeader("WL-Proxy-Client-IP"); 
        } 
        if(client_ip == null || client_ip.length() == 0 || "unknown".equalsIgnoreCase(client_ip)) { 
    	   client_ip = request.getRemoteAddr(); 
        }
        return client_ip;
	}
	

	public static String getremortIPAddr(){
		Enumeration allNetInterfaces = null;
		try {
			allNetInterfaces = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException e) {
			System.out.print(e.getMessage());
		}
		InetAddress ip = null;
		while (allNetInterfaces.hasMoreElements())
		{
			NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
			Enumeration addresses = netInterface.getInetAddresses();
			while (addresses.hasMoreElements())
			{
				ip = (InetAddress) addresses.nextElement();
				if (ip != null && ip instanceof Inet4Address)
				{
					if(!ip.getHostAddress().equals("127.0.0.1")){
						return ip.getHostAddress();
					}
				} 
			}
		}
		return null;
	}



}
